var indexPageUrl = '/pages/tabBar/home/home'
import {
	post
} from "./index.js"

export async function initNav(options = {}) {
	var t = uni.getStorageSync('USER_TOKEN');
	var q = uni.getStorageSync('alreadyOpen');
	var isShangjia = true;
	// 关闭启动图
	function closeLoading() {
		// console.debug('close')
		// #ifdef APP || APP-PLUS
		plus.navigator.closeSplashscreen()
		// #endif
	}
	// 正常认证
	function zhengchangrenzheng() {
		if (q) {

			if (options.path == 'pages/tabBar/qidong/qidong') {
				if (t) {
					uni.reLaunch({
						url: indexPageUrl,
						complete: closeLoading
					})
				} else uni.redirectTo({
					url: '/pages/tabBar/login/login',
					complete: closeLoading
				})
			} else closeLoading()
		} else {
			if (options.path == 'pages/tabBar/qidong/qidong') {
				uni.reLaunch({
					url: '/pages/guidance/guidance',
					fail(err) {
						console.debug(err, 'to guidance error')
					},
					complete: closeLoading
				})
			} else closeLoading()
		}
	}

	// #ifdef APP||APP-PLUS
	try {
		const res = await post("6584ef4d06588")
		if ("android" == uni.getSystemInfoSync().platform) {
			isShangjia = res.data.android_is_hide;
			if (isShangjia) {
				uni.setTabBarItem({
					index: 0,
					visible: false,
				})
				uni.setTabBarItem({
					index: 1,
					visible: false,
				})
				uni.setTabBarItem({
					index: 3,
					visible: false,
				})
				if (t) {
					if (options.path == 'pages/tabBar/qidong/qidong')
						uni.reLaunch({
							url: "/pages/tabBar/souti/souti",
							complete: closeLoading
						})
					else closeLoading()
				} else {
					uni.redirectTo({
						url: '/pages/tabBar/login/login',
						complete: closeLoading
					})
				}
			} else zhengchangrenzheng()
			return;
		} else {
			isShangjia = !res.data.is_hide
		}
	} catch (e) {
		return
	}
	// #endif



	// #ifdef MP-WEIXIN
	const iosEnv = false;
	q = 1;
	// #endif
	// #ifndef MP-WEIXIN
	const iosEnv = uni.getSystemInfoSync().platform != "android";
	// #endif

	if (q || iosEnv || isShangjia) {
		if (options.path == 'pages/tabBar/qidong/qidong') {
			if (t) {
				let url;
				// #ifdef MP-WEIXIN
				url = "/pages/tabBar/souti/souti";
				// #endif
				// #ifndef MP-WEIXIN
				url = indexPageUrl;
				// #endif
				uni.reLaunch({
					url,
					complete: closeLoading
				})
			} else uni.redirectTo({
				url: '/pages/tabBar/login/login',
				complete: closeLoading
			})
		} else closeLoading()
	} else {
		// #ifdef MP-WEIXIN
		uni.redirectTo({
			url: '/pages/tabBar/login/login',
			complete: closeLoading
		})
		// #endif
		// #ifndef MP-WEIXIN
		if (isShangjia)
			uni.redirectTo({
				url: '/pages/tabBar/login/login',
				complete: closeLoading
			})
		else {
			uni.reLaunch({
				url: '/pages/guidance/guidance',
				fail(err) {
					console.debug(err, 'to guidance error')
				},
				complete: closeLoading
			})
		}
		// #endif
	}
}